import {Component, ElementRef, OnInit, ViewChild} from '@angular/core';
import {User} from "../model/user";
import {LoginService} from "./login.service";
import {Observable} from "rxjs";
import {Router} from "@angular/router";

@Component({
  selector: 'app-login',
  templateUrl: './login.component.html',
  styleUrls: ['./login.component.css']
})
export class LoginComponent implements OnInit {

  @ViewChild('loginContent')
  loginContent: ElementRef;

  public user: User = new User();
  public error: Error;
    dataSource: Observable<any>;

  constructor(public loginService: LoginService,public router: Router) { }

  ngOnInit() {
     this.loginContent.nativeElement.style.minHeight = window.outerHeight;
  }

  public doLogin():void{
    this.dataSource = this.loginService.login(this.user);
    this.dataSource.subscribe(
      data => {
        let userJ = data.json();
        console.log({data:data.json()});
        if(userJ.data.status==1){
          console.log('用户登录成功');
          sessionStorage.setItem("currentUser",JSON.stringify(this.user));
          this.router.navigateByUrl('/reactor');
        }else {
          console.log('用户登录失败'+userJ.data.msg);
          this.error=userJ.data.msg;
        }
      },
      error => {
        console.error(error);
      }
    )
  }

}
